package com.water.camera.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.bean.EditItem;
import com.water.camera.databinding.DialogEditTitleContentBinding;

import org.json.JSONArray;
import org.json.JSONObject;

public class AddWordDlg extends FullScreenDlg {

    DialogEditTitleContentBinding binding;
    String key;
    EditItem item;

    public <T extends AddWordDlg> T setKey(String key, EditItem item) {
        this.key = key;
        this.item = item;
        return (T) this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogEditTitleContentBinding.inflate(inflater, container, false);
        binding.ivClose.setOnClickListener(v -> dismiss());
        binding.btnSave.setOnClickListener(v -> {
            save();
        });
        if (item != null) {
            binding.etTitle.setText(item.name);
            binding.etContent.setText(item.value);
            binding.delete.setVisibility(View.VISIBLE);
            binding.delete.setOnClickListener(v -> {
                delete();
            });
        }

        return binding.getRoot();
    }

    protected void delete() {
        String s = SPHelper.getInstance(getContext()).getString(key + "_word", "[]");
        JSONArray obj;
        try {
            obj = new JSONArray(s);
        } catch (Throwable e) {
            obj = new JSONArray();
        }
        for (int i = 0; i < obj.length(); i++) {
            try {
                JSONObject o = obj.getJSONObject(i);
                if (o.getString("key").equals(item.key)) {
                    obj.remove(i);
                    break;
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        SPHelper.getInstance(getContext()).putStringAndCommit(this.key + "_word", obj.toString());
        dismiss(true, null);
    }

    protected void save() {
        String title = binding.etTitle.getText().toString().trim();
        String desc = binding.etContent.getText().toString().trim();
        if (TextUtils.isEmpty(title)) {
            dismiss();
        } else {
            String s = SPHelper.getInstance(getContext()).getString(key + "_word", "[]");
            JSONArray obj;
            try {
                obj = new JSONArray(s);
            } catch (Throwable e) {
                obj = new JSONArray();
            }
            JSONObject tmp = null;
            String key = null;
            if (item != null) {
                key = item.key;
                for (int i = 0; i < obj.length(); i++) {
                    try {
                        JSONObject o = obj.getJSONObject(i);
                        if (o.getString("key").equals(item.key)) {
                            tmp = o;
                            break;
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }
            if (TextUtils.isEmpty(key)) {
                key = System.currentTimeMillis() + "";
            }
            if (tmp == null) {
                tmp = new JSONObject();
                try {
                    tmp.put("key", key);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                obj.put(tmp);
            }
            try {
                tmp.put("name", title);
                tmp.put("desc", desc);
                SPHelper.getInstance(getContext()).putStringAndCommit(this.key + "_word", obj.toString());
                if (item != null) {
                    item.name = title;
                    item.value = desc;
                } else {
                    item = new EditItem(key, title, desc);
                }
                dismiss(true, item);
            } catch (Throwable e) {
                e.printStackTrace();
                dismiss();
            }
        }
    }

    @Override
    protected int getHeight() {
        getDialog().getWindow().setGravity(Gravity.BOTTOM);
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }
}
